package com.kingeid.framework.modules.cms.web.front;

import com.kingeid.framework.common.utils.StringUtils;
import com.kingeid.framework.common.web.BaseController;
import com.kingeid.framework.modules.cms.entity.Article;
import com.kingeid.framework.modules.cms.entity.Site;
import com.kingeid.framework.modules.cms.service.ArticleDataService;
import com.kingeid.framework.modules.cms.service.ArticleService;
import com.kingeid.framework.modules.cms.service.CategoryService;
import com.kingeid.framework.modules.cms.utils.CmsUtils;
import com.kingeid.framework.modules.sys.entity.User;
import com.kingeid.framework.modules.sys.utils.UserUtils;
import com.kingeid.website.modules.slideshow.entity.WebsiteSlideshow;
import com.kingeid.website.modules.slideshow.service.WebsiteSlideshowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "${frontPath}/news")
public class FrontNewsController extends BaseController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleDataService articleDataService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private WebsiteSlideshowService websiteSlideshowService;

    @ModelAttribute
    public Model get(Model model) {
        Site site = CmsUtils.getSite((String)UserUtils.getCache("siteId"));
        model.addAttribute("site", site);

        // 版权所有
        WebsiteSlideshow websiteSlideshow = new WebsiteSlideshow();
        websiteSlideshow.setSsType("3");
        List<WebsiteSlideshow> lstCopyRightSlide = websiteSlideshowService.findList(websiteSlideshow);
        model.addAttribute("lstCopyRightSlide", lstCopyRightSlide);

        return model;
    }

    @RequestMapping(value = "detail/{mainId}${urlSuffix}")
    public String detail(@PathVariable String mainId, Model model, HttpServletRequest request, HttpServletResponse response) {
        Site site = CmsUtils.getSite((String) UserUtils.getCache("siteId"));
        model.addAttribute("site", site);
        Article art=new Article();
        art.setId(mainId);
        User loginU=UserUtils.getUser();
        if(StringUtils.isEmpty(loginU.getId())){
            loginU.setId("1");
        }
        art.setUser(loginU);
        Article article = articleService.get(art);
        article.setArticleData(articleDataService.get(article.getId()));
        model.addAttribute("article", article);
        articleService.updateHitsAddOne(mainId);

        List<Object[]> preNext=articleService.findPreAndNext(article);
        model.addAttribute("preNext", preNext);
        model.addAttribute("preNextSize", preNext.size());

        return "modules/cms/front/themes/"+site.getTheme()+"/details";
    }


}
